home *** CD-ROM | disk | FTP | other *** search
- /* from Dale Schumacher's dLibs */
-
- #include <stddef.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <memory.h>
- #include "lib.h"
-
- #define _NFILE NFILES
-
- FILE _iob[_NFILE] = /* stream buffers */
- {
- /* stdin */ {0, NULL, NULL, (_IOREAD | _IOFBF), 0, 0, '\0'},
- /* stdout */ {0, NULL, NULL, (_IOWRT | _IOLBF), 1, 0, '\0'},
- /* stderr */ {0, NULL, NULL, (_IOWRT | _IONBF), 2, 0, '\0'},
- {0, NULL, NULL, 0, 0, 0, '\0'},
- {0, NULL, NULL, 0, 0, 0, '\0'},
- {0, NULL, NULL, 0, 0, 0, '\0'},
- {0, NULL, NULL, 0, 0, 0, '\0'},
- {0, NULL, NULL, 0, 0, 0, '\0'},
- {0, NULL, NULL, 0, 0, 0, '\0'},
- {0, NULL, NULL, 0, 0, 0, '\0'},
- {0, NULL, NULL, 0, 0, 0, '\0'},
- {0, NULL, NULL, 0, 0, 0, '\0'},
- {0, NULL, NULL, 0, 0, 0, '\0'},
- {0, NULL, NULL, 0, 0, 0, '\0'},
- {0, NULL, NULL, 0, 0, 0, '\0'},
- {0, NULL, NULL, 0, 0, 0, '\0'},
- {0, NULL, NULL, 0, 0, 0, '\0'},
- {0, NULL, NULL, 0, 0, 0, '\0'},
- {0, NULL, NULL, 0, 0, 0, '\0'}
- };
-
- size_t __DEFAULT_BUFSIZ__;
-
- void _main(_argc, _argv, _envp)
- long _argc;
- char **_argv, **_envp;
- {
- void exit(int);
- register FILE *f;
- void _getbuf(FILE *);
- int main(int, char **, char **);
-
- if(!__DEFAULT_BUFSIZ__) __DEFAULT_BUFSIZ__ = BUFSIZ;
-
- for(f = &_iob[0]; f < &_iob[3]; f++) /* flag device streams */
- {
- if(isatty(f->_file))
- f->_flag |= _IODEV;
- else
- if(f == stdout)
- { /* stdout re-directed, make it full buffered */
- f->_flag &= ~_IOLBF;
- f->_flag |= _IOFBF;
- }
- _getbuf(f); /* get a buffer */
- }
-
- (void)main((int) _argc, _argv, _envp); /* if main() returns... */
- exit(EXIT_SUCCESS); /* ...exit with OK status */
- }
-
- void _exit(status)
- int status;
- {
- (void) callm1(MM, EXIT, status, 0, 0, NIL_PTR, NIL_PTR, NIL_PTR);
- }
-
- /* functions registered by user for calling at exit */
- #ifdef __STDC__
- typedef void (*ExitFn)(void);
- #else
- typedef void (*ExitFn)();
- #endif
- static ExitFn *_at_exit;
- static int num_at_exit = 0; /* number of functions registered - 1 */
-
- void exit(status)
- int status;
- {
- register int i, f;
-
- for(i = num_at_exit - 1; i >= 0; --i)
- {
- (*_at_exit[i])();
- }
- for(i=0; i<_NFILE; ++i)
- {
- f = _iob[i]._flag;
- if(f & (_IORW | _IOREAD | _IOWRT))
- fclose(&_iob[i]);
- }
- _exit(status);
- }
-
- /* register a function for execution on termination */
- /* Ansi requires atleast 32 entries, we make it dynamic and hope
- it meets the ansi requirement */
-
- int atexit(func)
- ExitFn func;
- {
- if (num_at_exit == 0)
- _at_exit = (ExitFn *)malloc((size_t)sizeof(ExitFn));
- else
- _at_exit = (ExitFn *)realloc(_at_exit, (size_t)((num_at_exit + 1) *
- sizeof(ExitFn)));
- if(_at_exit == (ExitFn *)NULL)
- return -1; /* failure */
-
- _at_exit[num_at_exit++] = func;
- return 0; /* success */
- }
-